import allure
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec


class BasePage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(self.driver, 10)

    @allure.step("打开浏览器")
    def open_browser(self, url):
        self.driver.get(url)

    # @allure.step("定位元素")
    # def locator_element(self, locator):
    #     element = self.driver.find_elements(*locator)
    #     return element

    def locator_station(self, element):
        self.driver.execute_script(
            "arguments[0].setAttribute('style',arguments[1]);",
            element,
            "border: 2px solid red;"
        )
    @allure.step("定位元素")
    def locator_element_with_wait(self, locator):
        element = self.wait.until(ec.visibility_of_element_located(locator))
        self.locator_station(element)
        return element

    @allure.step("定位多个元素")
    def locator_elements_with_wait(self, locator):
        elements = self.wait.until(ec.visibility_of_all_elements_located(locator))
        return elements

    @allure.step("切换窗口")
    def change_window(self, n):
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[n])

    @allure.step("等待文本出现")
    def text_wait(self, locator, txt):
        result = self.wait.until(ec.text_to_be_present_in_element(locator, str(txt)))
        return result

    @allure.step("输入内容")
    def send_keys(self, locator, txt):
        self.locator_element_with_wait(locator).send_keys(txt)

    @allure.step("点击")
    def click(self, locator):
        self.locator_element_with_wait(locator).click()

    @allure.step("获取文本")
    def get_text(self, locator):
        txt = self.wait.until(ec.visibility_of_element_located(locator)).text
        return txt

    @allure.step("获取元素属性值")
    def get_attribute_value_text(self,locator,):
        txt = self.wait.until(ec.visibility_of_element_located(locator)).get_attribute("value")
        return txt